package com.rem.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rem.mp.enums.SexEnum;
import com.rem.mp.mapper.UserMapper;
import com.rem.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 通用枚举
 * <p>
 * 解决了繁琐的配置，让 mybatis 优雅的使用枚举属性！
 *
 * @Author Rem
 * @Date 2020-03-01
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTest9 {

    @Autowired
    private UserMapper userMapper;


    /**
     * 枚举  新增
     * INSERT INTO tb_user(user_name, password, name, age, sex) VALUES  ('diaochan','888888','貂蝉',18,2)
     */
    @Test
    public void testInsert() {
        User user = new User();
        user.setName("貂蝉");
        user.setUserName("diaochan");
        user.setAge(18);
        user.setSex(SexEnum.WOMAN);
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

    /**
     * 枚举值也可以在wrapper条件中使用  value和dest会相互转换
     * SELECT id, user_name, name, age, email    AS mail, version, deleted, sex FROM tb_user WHERE deleted = 0 AND sex = 2
     */
    @Test
    public void testSelect() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("sex", SexEnum.WOMAN);

        List<User> users = userMapper.selectList(wrapper);
        //User(id=19, userName=diaochan, password=null, name=貂蝉, age=18, mail=null, address=null, version=1, deleted=0, sex=女)
        System.out.println(users.toString());
    }

}
